//
//  balancePaymentViewController.m
//  随便点
//
//  Created by MWJ on 15/4/13.
//  Copyright (c) 2015年 qfsoft. All rights reserved.
//

#import "balancePaymentViewController.h"
#import "AppDelegate.h"
#import "orderViewController.h"
#define Kyuezf @"http://www.51anys.com:8080/waimaiwang/order/balance"
#define Kyanzhengma @"http://www.51anys.com:8080/waimaiwang/user/validate/balance"

@interface balancePaymentViewController (){
    AppDelegate *app;
    NSString *BigString;
    NSString *YZMString;
    UIAlertView *_alert;
}

@end

@implementation balancePaymentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.hidesBackButton = YES;
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed: @"back.png"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = item;
    app = (AppDelegate *)[[UIApplication sharedApplication]delegate];
    self.PhoneLabel.layer.borderWidth = 1;
    self.YuELabel.text = app.YuE;
    self.PhoneLabel.text = app._phone;
    self.totalPrice.text = app.BuyPrice;
    // Do any additional setup after loading the view.
}
-(void)back{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)loading{
    NSString *dingdid;
    if (app.querId == nil||[app.querId isEqualToString:@""]) {
        dingdid = app.xhlddid;
    }else{
        dingdid = app.querId;
    }
    NSURL *url=[NSURL URLWithString:Kyuezf];
    NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
    NSString *postStr=[NSString stringWithFormat:@"orderId=%@&number=%@",dingdid,self.textView.text];
    [request setHTTPMethod:@"post"];
    NSData *data=[postStr dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:data];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError){
        BigString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        [self performSelectorOnMainThread:@selector(refreshData) withObject:BigString waitUntilDone:YES];
        if ([BigString isEqualToString:@"1"]) {
            UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"提示：" message:@"付款成功" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            _alert = alert;
            [alert show];
        }else if([BigString isEqualToString:@"0"]){
            UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"提示：" message:@"验证码错误" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            [alert show];
        }else{
            UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"提示：" message:@"余额不足，付款失败" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            [alert show];
        }
    }];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex==0&&alertView==_alert) {
        orderViewController *order = [self.storyboard instantiateViewControllerWithIdentifier:@"orderviewCV"];
        [self.navigationController pushViewController:order animated:YES];
    }
}
-(void)HuoQu{
    NSURL *url=[NSURL URLWithString:Kyanzhengma];
    NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
    NSString *postStr=[NSString stringWithFormat:@"userId=%@&phone=%@",app._id,self.PhoneLabel.text];
    [request setHTTPMethod:@"post"];
    NSData *data=[postStr dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:data];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError){
        YZMString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        [self performSelectorOnMainThread:@selector(refreshData) withObject:YZMString waitUntilDone:YES];
        if ([YZMString isEqualToString:@"1"]) {
            UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"提示：" message:@"发送成功" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            [alert show];
        }else{
            UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"提示：" message:@"发送失败" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            [alert show];
        }
    }];
}
-(void)refreshData{
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)PayCheckButton:(id)sender {
    
    [self loading];
}

- (IBAction)getNumber:(id)sender {
    [self HuoQu];
}
@end
